package Utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import Objects.CObject;
import Objects.CQuadTree;


public class CFile {
private static CFile instance = null;
	
	public static final CFile getInstance() {
		if(instance == null) {
			instance = new CFile();
		}
		return instance;
	}
	
	// Constructor
	public CFile() {
		// TODO Auto-generated constructor stub
	}
	
	// Read file
	public void readFile(CQuadTree quadTree, String fileName)  {
		try {
			FileInputStream f = new FileInputStream(new File(fileName));
			BufferedReader bf = new BufferedReader(new InputStreamReader(f));
			// rewind(f); //  Set pointer position to the head of the file
			if(bf != null) {
				int sizeTile = GameInformation.getInstance().getSizeTile();
				String base;
				base = bf.readLine();
				// 48 = '0'
				int width = (base.charAt(0) - 48)*100 + (base.charAt(1) - 48)*10 + (base.charAt(2) - 48);
				base = bf.readLine();
				int height = (base.charAt(0) - 48)*100 + (base.charAt(1) - 48)*10 + (base.charAt(2) - 48);
				// Multiplying with SizeTile cuz' need to suit with each unit
				quadTree.m_root.m_rect.setBottom(height*sizeTile);
				quadTree.m_root.m_rect.setRight(width*sizeTile);
				int row = 0;
				int column = 0;
				while(bf.readLine() != null) {
					base = bf.readLine();
					if(base.charAt(column) > GameInformation.getInstance().getUnit_Blank()) {
						// '0'
						if(base.charAt(column) == GameInformation.getInstance().getUnit_Begin()) {
							// '3'
							quadTree.m_fX = (float)column*sizeTile;
							quadTree.m_fY = (float)row*sizeTile;
						} else {
							quadTree.addObject(new CObject((float)column*sizeTile,(float)row*sizeTile,base.charAt(column)));
						}
					}
					column++;
				}
				row++;
				column = 0;
			}
			bf.close();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
		}
	}
	
	// Save file
	public void saveFile(int currentMap, float x, float y) {
		try {
			int sizeTile = GameInformation.getInstance().getSizeTile();
			FileOutputStream fos = new FileOutputStream("Map\\Save.txt", false);
			PrintWriter pw = new PrintWriter(fos);
			pw.println(currentMap);
			pw.println((int)x/sizeTile);
			pw.println((int)y/sizeTile);
			fos.close();
			
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
		}
	}
	
	// Read saved file
	
	public void readSavedFile(int currentMap, float x, float y) {
		try {
			int sizeTile = GameInformation.getInstance().getSizeTile();
			FileInputStream fis = new FileInputStream("Map\\Save.txt");
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
			currentMap = Integer.parseInt(br.readLine());
			x = (float)Integer.parseInt(br.readLine())*sizeTile;
			y = (float)Integer.parseInt(br.readLine())*sizeTile;
			fis.close();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}
